package cn.flying.cloud.base.common.utils.crypto;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * BASE64 编解码工具类
 *
 * @author: admin
 * @date: 2025年01月22日 11:17
 * @version: 1.0
 */
public class BASE64Util {
    private static final Logger logger = LoggerFactory.getLogger(BASE64Util.class);

    /**
     * BASE64 编码，字符串
     *
     * @param data
     * @return
     */
    public static String encode(String data) {
        if (data == null || "".equals(data.trim())) {
            return "";
        }
        return encode(data.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * BASE64 编码，字节数组
     *
     * @param data
     * @return
     */
    public static String encode(byte[] data) {
        if (data == null || data.length == 0) {
            return "";
        }
        return Base64.getEncoder().encodeToString(data);
    }

    /**
     * BASE64 解码
     *
     * @param data
     * @return
     */
    public static String decode(String data) {
        if (data == null || "".equals(data.trim())) {
            return "";
        }
        byte[] decoderByte = Base64.getDecoder().decode(data);
        return new String(decoderByte, StandardCharsets.UTF_8);
    }
}